package org.softmed.action.implementation;

import groovy.lang.Binding;
import groovy.lang.Script;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;

import org.softmed.action.ActionStep;
import org.softmed.action.exception.SimpleException;
import org.softmed.action.exception.StopException;
import org.softmed.action.workarea.WorkArea;
import org.softmed.filehandling.FileUtil;
import org.softmed.groovy.utils.GroovyCache;

public class GroovyScriptActionStep implements ActionStep, Cloneable {

	static List<String> imports = new ArrayList<String>();
	static String importLines;

	GroovyCache groovyCache;
	FileUtil util = new FileUtil();

	String filePath;
	private WorkArea workArea;

	public GroovyScriptActionStep(String scriptPath, GroovyCache groovyCache) {
		this.filePath = scriptPath;
		this.groovyCache = groovyCache;
		// buildImports();
	}

	public static void buildImports() {
		importLines = "";
		for (String imp : imports) {
			importLines += imp + ";\n";
		}
		importLines += "import org.softmed.action.exception.*;\n";
	}

	@Override
	public void dispose() throws Throwable {
		// TODO Auto-generated method stub

	}

	@Override
	public void execute() throws Throwable {

		try {

			Binding binding = (Binding) workArea.getAttribute("binding");

			if (binding == null) {
				binding = new Binding();

				Set<Entry<String, Object>> entries = workArea.getValues()
						.entrySet();
				for (Entry<String, Object> entry : entries) {
					binding.setVariable(entry.getKey(), entry.getValue());
				}

				workArea.setAttribute("binding", binding);
			}

			Script script = null;
			script = groovyCache.getCustomScript(filePath, importLines);

			script.setBinding(binding);
			script.run();

		} catch (Throwable t) {

			if (t instanceof StopException || t instanceof SimpleException)
				throw t;

			System.err.println("Error executing script file: " + filePath);

			t.printStackTrace();
			throw t;
		}

		//
		// File file = null;
		// try {
		//
		// file = util.getFile(script);
		// String code = util.readFromFile(file);
		// code = importLines + code;
		// Script sc = GroovyCache.getScripts().get(script);
		// if (sc == null) {
		//
		// GroovyShell shell = new GroovyShell(gse.getParentClassLoader(),
		// binding);
		// sc = shell.parse(code);
		//
		// GroovyCache.getScripts().put(script, sc);
		//
		// // shell.evaluate(code);
		//
		// }

		// sc.setBinding(binding);
		// sc.run();
		// gse.run(script, binding);

		// } catch (Throwable t) {
		//
		// if (t instanceof StopException || t instanceof SimpleException)
		// throw t;
		//
		// // System.err.println("Error executing script file: " + script);
		// // System.err.println("Full path to script file: "
		// // + file.getCanonicalPath());
		//
		// t.printStackTrace();
		// // throw new RuntimeException();
		// throw t;
		// }

	}

	@Override
	public void setWorkArea(WorkArea map) {
		this.workArea = map;

	}

	public GroovyScriptActionStep clone() {
		GroovyScriptActionStep clone = new GroovyScriptActionStep(filePath,
				groovyCache);
		return clone;
	}

	public static void addImport(String imp) {
		imports.add(imp);
		buildImports();
	}

}
